package com.xgh.offer;

import com.xgh.ListNode;

/**
 * @ClassName Offer06
 * @Description
 * @Author xinggh
 * @Date 2020/6/29 10:04
 * @Version 1.0
 **/
public class Offer06 {
    /**
     * 剑指 Offer 06. 从尾到头打印链表
     * 输入一个链表的头节点，从尾到头反过来返回每个节点的值（用数组返回）。
     * <p>
     * <p>
     * <p>
     * 示例 1：
     * <p>
     * 输入：head = [1,3,2]
     * 输出：[2,3,1]
     * <p>
     * <p>
     * 限制：
     * <p>
     * 0 <= 链表长度 <= 10000
     */
    public static int[] reversePrint(ListNode head) {
        if (head == null) {
            return new int[0];
        }
        int len = 0;
        ListNode p = head;
        while (p.next != null) {
            len++;
            p = p.next;
        }
        int[] result = new int[len + 1];
        int i = len;
        while (head.next != null) {
            result[i] = head.val;
            i--;
            head = head.next;
        }
        result[i] = head.val;
        return result;
    }

    public static void main(String[] args) {
        ListNode l1 = new ListNode(1);
        ListNode l2 = new ListNode(2);
        ListNode l3 = new ListNode(3);
        l1.next = l2;
        l2.next = l3;
        ListNode l4 = new ListNode();
        System.out.println(l4);

    }
}
